package tank;

import gameBasics.GameModel;
import gameBasics.ResourceManager;
import java.awt.*;

public class EnemyTank extends Tank{

    private int oldX, oldY;     //上一位置的坐标

    public EnemyTank(TankType tankType, int x, int y, Dir dir) {
        super(tankType, x, y, dir);

        GameModel.getInstance().addGameObject(this);
    }

    /**
     * 绘制敌对坦克
     * @param graphics 画笔对象
     */
    @Override
    public void paint(Graphics graphics) {
        if(!livingState) GameModel.getInstance().removeGameObject(this);

        switch (tankType){
            case ENEMY1 -> {
                switch (dir){
                    case LEFT -> graphics.drawImage(ResourceManager.enemyTank_1L, x, y, null);
                    case UP -> graphics.drawImage(ResourceManager.enemyTank_1U, x, y, null);
                    case RIGHT -> graphics.drawImage(ResourceManager.enemyTank_1R, x, y, null);
                    case DOWN -> graphics.drawImage(ResourceManager.enemyTank_1D, x, y, null);
                }
            }
            case ENEMY2 -> {
                switch (dir){
                    case LEFT -> graphics.drawImage(ResourceManager.enemyTank_2L, x, y, null);
                    case UP -> graphics.drawImage(ResourceManager.enemyTank_2U, x, y, null);
                    case RIGHT -> graphics.drawImage(ResourceManager.enemyTank_2R, x, y, null);
                    case DOWN -> graphics.drawImage(ResourceManager.enemyTank_2D, x, y, null);
                }
            }
            case ENEMY3 -> {
                switch (dir){
                    case LEFT -> graphics.drawImage(ResourceManager.enemyTank_3L, x, y, null);
                    case UP -> graphics.drawImage(ResourceManager.enemyTank_3U, x, y, null);
                    case RIGHT -> graphics.drawImage(ResourceManager.enemyTank_3R, x, y, null);
                    case DOWN -> graphics.drawImage(ResourceManager.enemyTank_3D, x, y, null);
                }
            }
            case ENEMY4 -> {
                switch (dir){
                    case LEFT -> graphics.drawImage(ResourceManager.enemyTank_4L, x, y, null);
                    case UP -> graphics.drawImage(ResourceManager.enemyTank_4U, x, y, null);
                    case RIGHT -> graphics.drawImage(ResourceManager.enemyTank_4R, x, y, null);
                    case DOWN -> graphics.drawImage(ResourceManager.enemyTank_4D, x, y, null);
                }
            }
        }

        move();
    }

    /**
     * 敌方坦克移动过程中随机开火
     */
    @Override
    public void move() {
        //记录移动之前的位置
        oldX = x;
        oldY = y;
        super.move();

        if(random.nextInt(101) > 98) this.fire();   //敌方坦克在移动过程中有2%的概率开火
        enemyTankRandomDir();
    }

    /**
     * 敌方坦克随机移动
     */
    private void enemyTankRandomDir() {
        if(random.nextInt(100) > 95){
            this.dir = Dir.values()[random.nextInt(4)];
        }
    }

    /**
     * 敌方坦克回退
     */
    public void back(){
        this.x = oldX;
        this.y = oldY;
    }

}
